package com.tzy.order.controller;
import com.tzy.common.entity.User;
import com.tzy.order.config.RedisService;
import com.tzy.order.service.UserServiceRpc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.List;
/**
 *  订单控制器
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Resource//实例在启动类的 @EnableDiscoveryClient启动时创建
    private DiscoveryClient discoveryClient;
    @Resource
    private UserServiceRpc userServiceRpc;
    @Resource
    private RedisService redisService;
    @Resource
    RestTemplate restTemplate;
    @GetMapping("/feign")
    public User feign(){
        return userServiceRpc.getUserInfoById(1);
    }
    @PutMapping("/user/add")
    public int insertUser(@RequestBody User user){
        log.info("user="+user);
        return userServiceRpc.insert(user);
    }
    //用了@RequestContextHolde注解后@HystrixCommand就会失效
    //fallbackMethod(当发生异常时要执行的方法/应急计划/备用的方法) = 方法名
//    @HystrixCommand(fallbackMethod = "loadBalanceFallback")//fallback应急计划,减少,削减
    @GetMapping("/load/balance")
    public User loadBalance(){
        User user = restTemplate.getForObject("http://user/user/info/1", User.class);
        if(null!=user){
            System.out.println("通过负载均衡获取用户数据");
            return user;
        }
        System.out.println("用户节点失效");
        return null;
    }
//    @HystrixCommand(fallbackMethod = "loadBalanceFallback")//fallback减少，削减
//    @GetMapping("/load/balance/fallback")
    public User loadBalanceFallback(){
//        User user = restTemplate.getForObject("http://user/user/info/3", User.class);
        System.out.println("执行熔断或限流");
        return new User();
    }
    @GetMapping("/test2")
    public User test(String serviceId,String url,Class clazz){
        List<ServiceInstance> userInstancesList = discoveryClient.getInstances(serviceId);
        if(userInstancesList.size()>0){
            //真实环境由负载均衡策略决定:轮询:访问量最少的,随机,
            //默认情况是随机选一个出来
            ServiceInstance instance = userInstancesList.get(0);
            //格式化输出 ,获取通讯协议,主机,端口号
            System.out.println("instance.getPort()="+instance.getPort());
            String str = String.format("%s://%s:%s"+url+"%d",
                    instance.getScheme(),instance.getHost(),instance.getPort());
            System.out.println("url="+str);
//            for (ServiceInstance service:userInstancesList){
//
//            }
        }
        RestTemplate restTemplate = new RestTemplate();
        User forObject = restTemplate.getForObject("http:localhost:9203/user/info/1", User.class);

        return new User();
    }
    //通过 @PathVariable 可以将URL中占位符参数{xxx}绑定到处理器类的方法形参中 @PathVariable(“xxx“)
    @GetMapping(value = "/info/{id}")
    public User getUserInfoById(@PathVariable("id") int id){
        List<ServiceInstance> userInstancesList = discoveryClient.getInstances("USER");
        if(userInstancesList.size()>0){
            //真实环境由负载均衡策略决定:轮询:访问量最少的,随机,
            //默认情况是随机选一个出来
            ServiceInstance instance = userInstancesList.get(0);
            //格式化输出 ,获取通讯协议,主机,端口号
            System.out.println("instance.getPort()="+instance.getPort());
            String str = String.format("%s://%s:%s/user/info/%d",
                    instance.getScheme(),instance.getHost(),instance.getPort(),8);
            System.out.println("url="+str);
//            for (ServiceInstance service:userInstancesList){
//
//            }
        }
        RestTemplate restTemplate = new RestTemplate();
        User forObject = restTemplate.getForObject("http:localhost:9203/user/info/1", User.class);

        return new User();
    }
    @GetMapping("/discovery")//discovery n名词. 发现
    public List<String> testDiscoveryClient(){
        //获取发现服务中注册的指定模块
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("USER");
        for (ServiceInstance instance:serviceInstances){
            log.info("host="+instance.getHost()+",scheme通讯协议="+instance.getScheme());
        }
        //获取发现服务中所有注册的模块; 获取应用里所有已注册的服务
        List<String> services = discoveryClient.getServices();
        //遍历已注册的服务
        for (String str:services){
            log.info("services="+str);
        }
        return services;
    }
}
